<template>
    <el-container class="home-container">
        <el-header>
            <div>
                <img src="../assets/heima.png" alt="">
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click="logout">退出</el-button>
        </el-header>

        <el-container>
            <el-aside :width="isCollapse?'64px':'200px'">
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <el-menu :default-active="activePath" router :collapse-transition="false" background-color="#333744" text-color="#fff" active-text-color="#409eff" unique-opened :collapse="isCollapse">
                    <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
                        <template slot="title">
                            <i :class="'el-icon-'+item.path"></i>
                            <span>{{ item.authName }}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavStatus('/'+subItem.path)">
                            <i class="el-icon-menu"></i>
                            <span>{{ subItem.authName }}</span>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>

            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
import http from '../util/http'
export default {
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data () {
    return {
      menulist: [],
      isCollapse: false,
      activePath: ''
    }
  },
  methods: {
    logout () {
      // 清空token值，跳转到登陆页
      window.sessionStorage.clear()
      this.$router.push('/login')
    },

    async getMenuList () {
      const { data: res } = await http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    },

    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 点击二级菜单时的index
    saveNavStatus (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container{
    height: 100%;
}
.el-header{
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 0;
    color: #fff;
    font-size: 20px;
    >div{
        display: flex;
        align-items: center;
        span{
            margin-left: 15px;
        }
    }
}
.el-aside{
    background: #333744;
    .el-menu{
        border: none;
    }
}
.el-main{
    background: #eaedf1;
}
.toggle-button{
    background: #4a5064;
    font-size: 15px;
    color: #fff;
    text-align: center;
    line-height: 24px;
    letter-spacing: .2em;//设置文本字符的间距表现
    cursor: pointer;
}
</style>
